<template>
  <span>校园经历</span>
  <el-button text @click="isAddCE = true">添加校园经历</el-button>
  <el-dialog v-model="isAddCE" title="添加校园经历">
    <el-form ref="formRef" :model="addCEForm" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="addCEForm.name"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="addCEForm.description" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-input v-model="addCEForm.role"></el-input>
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
            v-model="addCEForm.startTime"
            placeholder="选择日期"
            type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
            v-model="addCEForm.endTime"
            placeholder="选择日期"
            type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addCE(addCEForm)">提交</el-button>
        <el-button type="info" @click="isAddCE = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <div v-for="pe in PEList" :key="pe.id">
    <el-descriptions :column="2" :title="pe.name" border class="des">
      <template #extra>
        <el-button v-show="!modifyDict[pe.id]" type="primary" @click="modifyDict[pe.id] = true">编辑</el-button>
        <el-popconfirm
            v-show="!modifyDict[pe.id]"
            cancel-button-text="取消"
            confirm-button-text="确定"
            title="确定删除吗?"
            @confirm="deleteCE(pe)"
        >
          <template #reference>
            <el-button type="danger">删除</el-button>
          </template>
        </el-popconfirm>
        <el-button v-show="modifyDict[pe.id]" type="info" @click="modifyDict[pe.id] = false">取消</el-button>
        <el-button v-show="modifyDict[pe.id]" type="success" @click="updateCE(pe)">保存</el-button>
      </template>
      <el-descriptions-item label="名称">
        <el-input v-show="modifyDict[pe.id]" v-model="pe.name"/>
        <span v-show="!modifyDict[pe.id]">{{ pe.name }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="担任角色">
        <el-input v-show="modifyDict[pe.id]" v-model="pe.role"/>
        <span v-show="!modifyDict[pe.id]">{{ pe.role }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="开始时间">
        <div v-show="modifyDict[pe.id]">
          <el-date-picker
              v-model="pe.startTime"
              placeholder="请选择"
              type="date"
          />
        </div>
        <span v-show="!modifyDict[pe.id]">{{
            pe.startTime instanceof Date ? pe.startTime.toLocaleDateString() : pe.startTime.substr(0, 10)
          }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="结束时间">
        <div v-show="modifyDict[pe.id]">
          <el-date-picker
              v-model="pe.endTime"
              placeholder="请选择"
              type="date"
          />
        </div>
        <span v-show="!modifyDict[pe.id]">{{
            pe.endTime instanceof Date ? pe.endTime.toLocaleDateString() : pe.endTime.substr(0, 10)
          }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="详情">
        <el-input v-show="modifyDict[pe.id]" v-model="pe.description" type="textarea"/>
        <div v-show="!modifyDict[pe.id]">{{ pe.description }}</div>
      </el-descriptions-item>
    </el-descriptions>
    <el-divider/>
  </div>
</template>

<script lang="ts" setup>
import type {
  CampusExperienceInterface,
  ProjectExperienceInterface,
  ResponseInterface,
  StudentInterface
} from "@/model/type";
import {onMounted, reactive, ref} from "vue";
import {
  addCampusExperienceAPI,
  deleteCampusExperienceAPI,
  getCampusExperiencesAPI,
  updateCampusExperienceAPI
} from "@/api/ResumeAPI";
import {ElMessage} from "element-plus";

const isAddCE = ref<boolean>(false)
const addCEForm = reactive<CampusExperienceInterface>({
  description: "",
  endTime: "",
  id: "",
  name: "",
  resumeId: "",
  role: "",
  startTime: "",
  studentId: ""
})
const PEList = reactive<Array<CampusExperienceInterface>>([]);
const modifyDict = reactive({"id": false})
const resumeId: string = localStorage.getItem("resumeId")
const student: StudentInterface = JSON.parse(localStorage.getItem("user"))

async function addCE(pe: ProjectExperienceInterface) {
  pe.resumeId = localStorage.getItem("resumeId")
  pe.studentId = student.id
  console.log(pe)
  const r: ResponseInterface = await addCampusExperienceAPI(pe, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("添加成功")
    PEList.push(r.data)
    Object.assign(modifyDict, {[r.data.id]: false})
    Object.assign(addCEForm, {
      description: "",
      endTime: "",
      id: "",
      name: "",
      resumeId: "",
      role: "",
      startTime: "",
      studentId: ""
    })
    isAddCE.value = false
  }
}

async function deleteCE(pe: ProjectExperienceInterface) {
  const r: ResponseInterface = await deleteCampusExperienceAPI(pe, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("删除成功")
    PEList.splice(PEList.indexOf(pe), 1)
  }
}

async function updateCE(pe: ProjectExperienceInterface) {
  const r: ResponseInterface = await updateCampusExperienceAPI(pe, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("修改成功")
    modifyDict[pe.id] = false
  }
}

onMounted(async () => {
  const r: ResponseInterface = await getCampusExperiencesAPI(resumeId, localStorage.getItem("token"))
  if (r.code == 200) {
    for (let i = 0; i < r.data.length; i++) {
      PEList.push(r.data[i])
      Object.assign(modifyDict, {[r.data[i].id]: false})
    }
  }
})
</script>
<style scoped>
.des {
  margin-top: 20px;
}
</style>